<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/6/29-16:41
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */

namespace App\Http\Controllers\Web;


use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

/**
 * 找项目列表 /find/message
 **/
class ProjectController extends BaseController
{
    protected $request;
    protected  $api_url = [
        'index'=> ['get', 'find/message'],
        'show' => ['get', 'find/message/%d']
    ];

    protected $project_time_seconds_map = [
        'three_day' => 3600 * 24 * 3,
        'week' => 3600 * 24 * 7,
        'half_month' => 3600 * 24 * 15,
        'month' => 3600 * 24 * 30
    ];
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 分类下文章列表
     * */
    public function index(){
        $search_params = $this->request->all();

        $search_params = [
            'city_id' => isset($search_params['city_id'])?$search_params['city_id']:null,
            'announcement_type' => isset($search_params['announcement_type'])?$search_params['announcement_type']:null,
            'purchase_mode' => isset($search_params['purchase_mode'])?$search_params['purchase_mode']:null,
            'keyword' => isset($search_params['keyword'])?$search_params['keyword']:null,
            'city_name' => isset($search_params['city_name'])?$search_params['city_name']:null,
            //'project_type' => isset($search_params['project_type'])?$search_params['project_type']:null,
        ];
        $key_time = $this->request->get('time',null);

        if (!empty($key_time) && array_key_exists($key_time,$this->project_time_seconds_map)) {
            $search_params['created_at']['start'] = date('Y-m-d H:i:s', time() - $this->project_time_seconds_map[$key_time]);
            $search_params['created_at']['end'] = date('Y-m-d H:i:s', time());
        }

        $page = $this->request->get('page',1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page,
            'type'=>'page',
            'with'=>'region'
        ];
//        dd($search_params);
        // 获取文章列表
        $ret = http_web($this->api_url['index'])->send($params);

        $data = paginate($this->request, $ret['data']);
        //$search_params['project_type'] = explode(',',$search_params['project_type']);
        $search_params['time'] = $this->request->get('time');
        $data['search_params'] = $search_params;
//        dd($data);
        return view('web.find.message')->with($data);
    }

    /**
     * 文章详情
     * */
    public function show($id){
        $ret  = http_web($this->api_url['show'],[$id])->send();
//        dd($ret);
        return view('web.find.message_details')->with($ret['data']);
    }

}